package com.twu.biblioteca.operation;

import com.twu.biblioteca.customer.Customer;
import com.twu.biblioteca.library.Library;

import java.util.List;
import java.util.Scanner;

public class LoginMenuOperation implements MenuOperation {

	@Override
	public String execute(Library library, Customer customer) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("Username:");
		String username = scanner.nextLine();
		System.out.println("Password:");
		String password = scanner.nextLine();
		return checkCustomer(library, username, password, customer);
	}

	public String checkCustomer(Library library, String username, String password, Customer customer) {
		List<Customer> customers = library.getCustomers();
		for (Customer customerNext : customers) {
			if (username.equals(customerNext.getName()) && password.equals(customerNext.getPassword())) {
				setCustomerInfo(customer, customerNext);
				return "Welcome, " + customer.getName();
			}
		}
		return "Login failed";
	}

	private void setCustomerInfo(Customer customer, Customer customerNext) {
		customer.setName(customerNext.getName());
		customer.setEmail(customerNext.getEmail());
		customer.setPhone(customerNext.getPhone());
	}
}
